package com.home.a555clock

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import java.util.UUID

class MainActivity2 : AppCompatActivity() {
  private val myWorkerManger = WorkManager.getInstance(this)
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity2_main)
    context = applicationContext
    findViewById<Button>(R.id.enqueueBtn).setOnClickListener {
      val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
      val workRequest = OneTimeWorkRequestBuilder<MyWorker>()
//        .setConstraints(constraints)
        .build()
      startTime = System.currentTimeMillis()
      // 在enqueue前，添加observe以监听状态
      myWorkerManger.getWorkInfoByIdLiveData(workRequest.id).observe({ lifecycle }) {
        Log.d("zzm debug", "periodic: ${it.id}: ${it.state}")
      }
      myWorkerManger.enqueue(workRequest)
    }

    /*findViewById<Button>(R.id.pause).setOnClickListener {
      workId?.let { it1 ->
        myWorkerManger.cancelWorkById(it1)
        workId = null
      }
      restTime -= (System.currentTimeMillis() - startTime!!)
    }
    findViewById<Button>(R.id.continu).setOnClickListener {
      val workRequest = OneTimeWorkRequestBuilder<MyWorker>().build()
      workId = workRequest.id
      startTime = System.currentTimeMillis()
      myWorkerManger.enqueue(workRequest)
    }
    findViewById<Button>(R.id.cancel).setOnClickListener {
      workId?.let { it1 ->
        myWorkerManger.cancelWorkById(it1)
        workId = null
      }
    }
    findViewById<Button>(R.id.notify).setOnClickListener {

    }
    findViewById<Button>(R.id.taobao).setOnClickListener {
      val packageName = "com.taobao.taobao"
      try {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.component = ComponentName(packageName, "com.taobao.tao.welcome.Welcome")
        startActivity(intent)
      } catch (e: Exception) {
        e.printStackTrace()
        Log.e("zzm debug", "未安装")
      }
    }*/
  }

  companion object {
    var workId: UUID? = null
    var startTime: Long? = null
    var restTime = 10 * 1000 * 1L
    var context:Context? = null

    fun notify(context: Context){
      //创建NotificationManager对通知进行管理
      val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      //Android O （8.0）以上版本需要渠道
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //最后一个参数是通知重要度，DEFAULT及以上，通知时手机默认会有振动
        val notificationChannel =
          NotificationChannel("channelid1", "channelname", NotificationManager.IMPORTANCE_HIGH)
        manager.createNotificationChannel(notificationChannel)
      }
      val intent = Intent()
      intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
      intent.component = ComponentName("com.taobao.taobao", "com.taobao.tao.welcome.Welcome")
      //创建Notification对象
      val pi =
        PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
      val builder = NotificationCompat.Builder(context, "channelid1")
      //设置刚刚的PendingIntent对象
      builder.setContentIntent(pi)
      builder.setContentTitle("ContentTitle")
      builder.setContentText("ContentText")
      //设置通知被创建的时间
      builder.setWhen(System.currentTimeMillis())
      //设置出现在状态栏的小图标
      builder.setSmallIcon(R.drawable.ic_chat)
      //设置通知的大图标
      builder.setLargeIcon(
        BitmapFactory.decodeResource(
          context.resources,
          R.drawable.ic_doctor
        )
      )
      builder.setAutoCancel(true)
      //让通知显示出来 第一个参数时id，要保证每个通知所指定的id各不相同 第二个参数是Notification对象
      manager.notify(1, builder.build())
    }
  }
}